#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import socket
import subprocess

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("127.0.0.1", 2003))
server.listen(3)

conn, addr = server.accept()
while True:
    data = conn.recv(1024)

    if data == "exit": exit(3)

    res = subprocess.Popen("{}".format(data.decode("utf-8")), shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    stdout = res.stdout.read()
    stderr = res.stderr.read()
    conn.send(stdout.decode("gbk").encode("utf-8"))
    # conn.send("{} 命令不存在".format(data.decode("utf-8")).encode("gbk"))
conn.close()

# res = subprocess.Popen("dir", shell=True,
#                  stdout=subprocess.PIPE,
#                  stderr=subprocess.PIPE)
